package com.nand.library.base.http;

import android.app.Activity;
import android.util.Log;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.nand.library.base.bean.ResponseBean;

import java.io.File;
import java.util.List;

/**
 * Created by 孝杰 on 2019/12/17.
 */
public class OkGoUtils<T> {
    private static final String TAG = "OkGoUtils";

    /**
     * 单列模式
     **/
    private static OkGoUtils mOkGoUtils = null;

    /**
     * 构造函数私有化
     **/
    private OkGoUtils() {
    }

    /**
     * 公有的静态函数，对外暴露获取单例对象的接口
     **/
    public static OkGoUtils getInstance() {
        if (mOkGoUtils == null) {
            synchronized (OkGoUtils.class) {
                if (mOkGoUtils == null) {
                    mOkGoUtils = new OkGoUtils();
                }
            }
        }
        return mOkGoUtils;
    }


    /**
     * get请求封装方法
     *
     * @param activity
     * @param url      请求url
     * @param params   参数
     * @param clazz    实体类
     * @param callback 回调
     */
    public void getByOkGo(Activity activity, String url,
                          HttpParams params, Class<T> clazz,
                          final Callback<T> callback) {
        OkGo
                // 请求方式和请求url
                .<T>get(url)
                .params(params)
                // 请求的 tag, 主要用于取消对应的请求
                .tag(this)
                // 设置当前请求的缓存key,建议每个不同功能的请求设置一个
                .cacheKey("cacheKey")
                // 缓存模式，详细请看缓存介绍
                .cacheMode(CacheMode.DEFAULT)
                .execute(new DialogCallback<T>(activity, clazz) {
                    @Override
                    public void onSuccess(Response<T> response) {
                        Log.i(TAG, "onSuccess: " + response);
                        callback.onSuccess(response.body(), 1);
                    }

                    @Override
                    public void onError(Response<T> response) {
                        super.onError(response);
                        Throwable throwable = response.getException();
                        if (throwable != null) {
                            throwable.printStackTrace();
                            callback.onError(throwable, 2);
                        }

                    }

                });
    }

    /**
     * post请求封装方法
     *
     * @param activity
     * @param url      请求url
     * @param params   参数
     * @param clazz    实体类
     * @param callback 回调
     *                 修改后推荐使用 post()方法
     */
    @Deprecated
    public void postByOkGo(Activity activity, String url,
                           HttpParams params, Class<T> clazz,
                           final Callback<T> callback, final int code) {
        OkGo
                // 请求方式和请求url
                .<T>post(url)
                .params(params)
                // 请求的 tag, 主要用于取消对应的请求
                .tag(this)
                // 设置当前请求的缓存key,建议每个不同功能的请求设置一个
                .cacheKey("cacheKey")
                // 缓存模式，详细请看缓存介绍
                .cacheMode(CacheMode.DEFAULT)
                .execute(new DialogCallback<T>(activity, clazz) {
                    @Override
                    public void onSuccess(Response<T> response) {
                        Log.i(TAG, "onSuccess: " + response);
                        callback.onSuccess(response.body(), code);
                    }

                    @Override
                    public void onError(Response<T> response) {
                        super.onError(response);
                        Throwable throwable = response.getException();
                        if (throwable != null) {
                            throwable.printStackTrace();
                            callback.onError(throwable, code);
                        }

                    }

                });
    }

    /**
     * @param activity
     * @param url      请求url
     * @param params   参数
     * @param code     区分同时请求回传
     * @param isDialog 是否显示加载框
     * @param callback 回调接口实现
     */
    public void post(Activity activity, String url, HttpParams params, final int code,
                     boolean isDialog, final MyCallback callback) {
        OkGo
                // 请求方式和请求url
                .<ResponseBean>post(url)
                .params(params)
                // 请求的 tag, 主要用于取消对应的请求
                .tag(this)
                // 设置当前请求的缓存key,建议每个不同功能的请求设置一个
                .cacheKey("cacheKey")
                // 缓存模式，详细请看缓存介绍
                .cacheMode(CacheMode.DEFAULT)
                .execute(isDialog ? new DialogCallback<ResponseBean>(activity, ResponseBean.class) {
                    @Override
                    public void onSuccess(Response<ResponseBean> response) {
                        Log.i(TAG, "onSuccess: " + response);
                        callback.onSuccess(response.body(), code);
                    }

                    @Override
                    public void onError(Response<ResponseBean> response) {
                        super.onError(response);
                        Throwable throwable = response.getException();
                        if (throwable != null) {
                            throwable.printStackTrace();
                            callback.onError(throwable, code);
                        }
                    }
                } : new JsonCallback<ResponseBean>(ResponseBean.class) {
                    @Override
                    public void onSuccess(Response<ResponseBean> response) {
                        Log.i(TAG, "onSuccess: " + response);
                        callback.onSuccess(response.body(), code);
                    }

                    @Override
                    public void onError(Response<ResponseBean> response) {
                        super.onError(response);
                        Throwable throwable = response.getException();
                        if (throwable != null) {
                            throwable.printStackTrace();
                            callback.onError(throwable, code);
                        }
                    }
                });
    }

    public void upload(Activity activity, String url, List<File> files, final MyCallback callback) {
        OkGo.<ResponseBean>post(url).tag(activity).addFileParams("files", files).execute(new DialogCallback<ResponseBean>(activity, ResponseBean.class) {
            @Override
            public void onSuccess(Response<ResponseBean> response) {
                callback.onSuccess(response.body(), 1);
            }

            @Override
            public void onError(Response<ResponseBean> response) {
                callback.onError(response.getException(), 2);
            }
        });
    }
}
